import 'javax.xml.validation.SchemaFactory'
import 'javax.xml.XMLConstants'
import 'java.net.URL'
import 'javax.xml.transform.stream.StreamSource'
import 'java.io.StringReader'
import 'org.xml.sax.SAXException'

class ValidatorController < ApplicationController

  SCHEMA = SchemaFactory.newInstance(XMLConstants::W3C_XML_SCHEMA_NS_URI).newSchema(URL.new('http://ias.lab01.openmaru.com/springnote.xsd'))

  def create
    source = StreamSource.new(StringReader.new(params[:page]))
    flash[:content] = params[:page]
    validator = SCHEMA.newValidator
    begin
      validator.validate(source)
      flash[:result] = 'valid'
    rescue SAXException => e
      flash[:result] = e.message
    end
    redirect_to :action => 'new'
  end
end